package cuong.ptit.androidvn.helper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;

/*
 * @author LeCuong 
 * @version 1.0
 */

public class Function {

	public static void CollectionSort(ArrayList<HashMap<String, String>> map,
			final String value) {

		Collections.sort(map, new Comparator<HashMap<String, String>>() {

			@Override
			public int compare(HashMap<String, String> map1,
					HashMap<String, String> map2) {
				return Integer.valueOf(map1.get(value)).compareTo(
						Integer.valueOf(map2.get(value)));
			}
		});

	}

	public static void CollectionSortBA(ArrayList<HashMap<String, String>> map,
			final String value) {

		Collections.sort(map, new Comparator<HashMap<String, String>>() {

			@Override
			public int compare(HashMap<String, String> map1,
					HashMap<String, String> map2) {
				return Integer.valueOf(map2.get(value)).compareTo(
						Integer.valueOf(map1.get(value)));
			}
		});

	}

	public static String getDate(long milliSeconds, String dateFormat) {
		// Create a DateFormatter object for displaying date in specified
		// format.
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);

		// Create a calendar object that will convert the date and time value in
		// milliseconds to date.
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(milliSeconds);
		return formatter.format(calendar.getTime());
	}

	// get name box

	public static String GetNameBox(
			ArrayList<HashMap<String, String>> listArray, String node_id) {
		String nameBox = "Box:";

		for (int i = 0; i < listArray.size(); i++) {
			HashMap<String, String> map = listArray.get(i);
			if (map.get("node_id").equals(node_id)) {
				nameBox = nameBox + map.get("title");
				break;
			}

		}

		return nameBox;
	}

	public static String GetComment(
			ArrayList<HashMap<String, String>> listArray, String thread_id) {
		String nameBox = "";

		for (int i = 0; i < listArray.size(); i++) {
			HashMap<String, String> map = listArray.get(i);
			if (map.get("thread_id").equals(thread_id)) {
				nameBox = nameBox + map.get("message");
				break;
			}

		}

		return nameBox;
	}

	// get name thread to id_thread

	public static String GetNameThread(
			ArrayList<HashMap<String, String>> listArray, String thread_id,
			String value) {

		String vl = value;
		String nameThread = "";

		for (int i = 0; i < listArray.size(); i++) {
			HashMap<String, String> map = listArray.get(i);
			if (map.get("thread_id").equals(thread_id)) {
				nameThread = nameThread + map.get(vl);
				break;
			}
		}
		return nameThread;
	}

	/*
	 * // get id node to id_thread public static String GetIdNode(
	 * ArrayList<HashMap<String, String>> listArray, String thread_id) { String
	 * nameThread = "";
	 * 
	 * for (int i = 0; i < listArray.size(); i++) { HashMap<String, String> map
	 * = listArray.get(i); if (map.get("thread_id").equals(thread_id)) {
	 * nameThread = nameThread + map.get("title"); break; } } return nameThread;
	 * }
	 */

	// get 20 word frist
	public static String getFrist10Word(String arg) {
		String a = arg.substring(arg.lastIndexOf("]") + 1);
		// String b=a.substring(a.lastIndexOf(".")+1);
		Pattern pattern = Pattern.compile("([\\S]+\\s*){1,20}");
		Matcher matcher = pattern.matcher(a);
		matcher.find();
		return matcher.group();
	}

	// remove item repeat in array list

	public static void RemoreItemRepeatArray(
			ArrayList<HashMap<String, String>> list) {
		Object[] st = list.toArray();
		for (Object s : st) {
			if (list.indexOf(s) != list.lastIndexOf(s)) {
				list.remove(list.lastIndexOf(s));
			}
		}
	}

}
